package nju.software.xkxt.data.dao.implement;

import java.util.List;

import nju.software.xkxt.data.dao.UserDao;
import nju.software.xkxt.data.dataobject.UserDO;

import org.apache.xmlbeans.impl.xb.xsdschema.impl.AttributeImpl.UseImpl;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * @author typ
 * 
 */
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	public boolean addUser(UserDO userDO) {
		boolean b = false;
		if (userDO != null) {
			try {
				getHibernateTemplate().save(userDO);
				b = true;
			} catch (Exception e) {
				b = false;
			}
		} else {
			b = false;
		}
		return b;
	}

	public boolean vertifyUser(String sch_num, String passwd) {
		String hql = "select count(*) from UserDO where schoolNo=? and pwd=?";
		List<Long> num = getHibernateTemplate().find(hql, sch_num, passwd);
		if (num.size() == 0) {
			return false;
		} else {
			Long integer = num.get(0);
			if (integer == 0) {
				return false;
			} else {
				return true;
			}
		}

	}

	public Integer getMax() {
		String hql = "select max(userId) from UserDO";
		Session session = this.getSession();
		Query query = session.createQuery(hql);
		int userId;
		if (query.uniqueResult() != null) {
			userId = (Integer) query.uniqueResult();
		} else {
			userId = 0;
		}
		session.close();
		return userId;
	}

	public UserDO getUserBySchoolNo(String sch_no) {
		String hql = "from UserDO where schoolNo=?";
		List<UserDO> userDOs = getHibernateTemplate().find(hql, sch_no);
		UserDO userDO = userDOs.get(0);
		return userDO;
	}

	public boolean vertifyPwd(String passwd) {
		String hql = "select count(*) from UserDO where schoolNo='admin' and pwd='"
				+ passwd + "'";
		List<Long> nums = getHibernateTemplate().find(hql);
		long num = nums.get(0);
		boolean b;
		if (num == 0)
			b = false;
		else
			b = true;
		return b;
	}

	public boolean alterPwd(String new_pwd) {
		String hql = "update UserDO set pwd='" + new_pwd
				+ "' where schoolNo='admin'";
		boolean b;
		try {
			Session session = this.getSession();
			Query query = session.createQuery(hql);
			session.beginTransaction();
			query.executeUpdate();
			session.getTransaction().commit();
			session.flush();
			session.close();
			b = true;
		} catch (Exception e) {
			b = false;
		}

		return b;
	}

	public List<UserDO> getUserByRole(int role) {
		String hql = "from UserDO where role=" + role;
		List<UserDO> userDOs = getHibernateTemplate().find(hql);
		return userDOs;
	}

	@Override
	public boolean deleteById(int userId) {
		boolean b;
		String hql = "delete from UserDO where userId=:userId";
		Session session = this.getSession();
		Query query = session.createQuery(hql);
		query.setInteger("userId", userId);
		try {
			query.executeUpdate();
			b = true;
		} catch (Exception e) {
			b = false;
		}
		return b;
	}

	@Override
	public UserDO getUserById(int userId) {
		String sqlString = "from UserDO where userId=?";
		List<UserDO> userDOs = getHibernateTemplate().find(sqlString, userId);
		return userDOs.size() == 0 ? null : userDOs.get(0);
	}
}
